// pages/home/subscribe/myList/myList.js
let app = getApp();
import * as user from '../../../../models/user'
import {
  dialog
} from './../../../../component/userStatusDialog/control'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _enterCount: 0,
    _title: '',
    _isRefreshVip: false,
    _isRefreshFollow: false,
    isHidePage: true,
    cityCode: '',
    communityID: '',
    cityCode: '',
    defaultTabSelect: '',
    tabSelect: '',
    tabList: [{
      id: 'sales',
      label: '二手',
      add: '',
    }, {
      id: 'rent',
      label: '出租',
      add: '',
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('mylist-options', options);
    this.initData(options)
    this.gioEnter(options)
    app.globalData.loginPromise.then(() => {
      app.mobileLogin(false, '', false)
        .then(() => {
          this.checkUserAuth()
        })
        .catch(() => {
          app.globalData.afterMobileAuthorCallBack = () => {
            wx.redirectTo({
              url: "/pages/home/subscribe/list/list"
            });
          };
        })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data._enterCount += 1
    wx.hideShareMenu()
    this.refreshVip()
    this.refreshFollow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      tabSelect
    } = this.data
    this.selectComponent(`#list-${tabSelect}`) && this.selectComponent(`#list-${tabSelect}`).onPageReachBottom()
  },

  initData(options) {
    wx.setNavigationBarTitle({
      title: 'xxx小区'
    })
    this.setData({
      _title: options.name || '',
      defaultTabSelect: options.tab || 'sales',
      tabSelect: options.tab || 'sales',
      communityID: options.id,
      cityCode: options.citycode || '',
      filterType: options.type,
      filterTime: options.time || ''
    })
  },

  onTabChange(e) {
    const {
      tabSelect
    } = this.data
    let target = e.detail
    if (tabSelect === target) return
    this.setData({
      filterType: '',
      tabSelect: target,
    })
  },

  // 更新vip状态
  refreshVip() {
    if (this.data._isRefreshVip) {
      this.data._isRefreshVip = false
      this.checkUserAuth()
        .then(() => {
          this.selectComponent('#user-status-dialog').clean()
        })
    }
  },

  // 更新公众号状态
  refreshFollow() {
    if (this.data._isRefreshFollow) {
      this.data._isRefreshFollow = false
      user.updateOfficialAccount()
        .then(() => {
          this.checkUserAuth()
            .then(() => {
              this.selectComponent('#user-status-dialog').clean()
            })
        })
    }
  },

  checkUserAuth() {
    return new Promise((resolve, reject) => {
      Promise.all([user.getVip(), user.getOfficialAccount()])
        .then((res) => {
          const vipStatus = res[0]
          const followStatus = res[1]
          if (vipStatus === '0') {
            wx.setNavigationBarTitle({
              title: !this.data._title || this.data._title == undefined ? '' : this.data._title,
            })
            this.setData({
              isHidePage: false,
            })
          }
          if (vipStatus !== '0') {
            switch (vipStatus) {
              case '1':
                dialog({
                  isKeep: true,
                  dialog: 'ban',
                  confirm: () => {}
                })
                break;
              case '2':
                dialog({
                  isKeep: true,
                  dialog: 'due',
                  confirm: () => {
                    this.data._isRefreshVip = true
                    wx.navigateTo({
                      url: '/pages/mine/memberCentre/memberCentre?orderEntr=新上房源详情',
                    })
                  }
                })
                break
              default:
                dialog({
                  isKeep: true,
                  dialog: 'vip',
                  confirm: () => {
                    this.data._isRefreshVip = true
                    wx.navigateTo({
                      url: '/pages/mine/memberCentre/memberCentre?orderEntr=新上房源详情',
                    })
                  }
                })
            }
            if (this.data._enterCount <= 1) {
              this.data._isRefreshVip = true
              wx.navigateTo({
                url: '/pages/mine/memberCentre/memberCentre?orderEntr=新上房源详情',
              })
            }
            return reject()
          }

          if (followStatus !== '1') {
            dialog({
              isKeep: true,
              dialog: 'public1',
              confirm: () => {
                this.data._isRefreshFollow = true
                let url = 'https://mp.weixin.qq.com/s/I8sdEESdeHIoKLyYVnDcYQ'
                wx.navigateTo({
                  url: `/pages/webView/webView?webViewUrl=${url}`
                })
              }
            })
            return reject()
          }
          resolve()
        })
        .catch(() => {
          reject()
        })
    })
  },

  gioEnter(options) {
    if ((wx.getLaunchOptionsSync() || {}).scene === 1043 && options.from === 'push') {
      wx.gio("track", 'fromOfficialAccount', {
        'subscribe_type': options.type
      });
    }
  },

  onTimeChange(e) {
    this.setData({
      filterTime: e.detail || ''
    })
  }


})